/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.sure.view.chart;

import com.factory.sure.chart.HistoryLineChartFXMLController;
import com.factory.sure.chart.LUHistoryLineChartAssistance;
import com.factory.sure.chart.exception.ChartModeException;
import java.awt.BorderLayout;
import java.util.Collection;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.Lookup;
import org.openide.windows.TopComponent;
import org.openide.util.NbBundle.Messages;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(
        dtd = "-//com.factory.sure.view.chart//LUHistoryLineChart//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "LUHistoryLineChartTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_NEVER)
@TopComponent.Registration(mode = "output", openAtStartup = false)
@ActionID(category = "Window", id = "com.factory.sure.view.chart.LUHistoryLineChartTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_LUHistoryLineChartAction",
        preferredID = "LUHistoryLineChartTopComponent")
@Messages({
    "CTL_LUHistoryLineChartAction=LUHistoryLineChart",
    "CTL_LUHistoryLineChartTopComponent=LUHistoryLineChart Window",
    "HINT_LUHistoryLineChartTopComponent=This is a LUHistoryLineChart window"
})
public final class LUHistoryLineChartTopComponent extends TopComponent {

    private JFXPanel m_pFXPanel;
    private LUHistoryLineChartAssistance m_pLUHistoryLineChartAssistance = null;

    public LUHistoryLineChartTopComponent() {
        initComponents();
        setName(Bundle.CTL_LUHistoryLineChartTopComponent());
        setToolTipText(Bundle.HINT_LUHistoryLineChartTopComponent());

        Collection<? extends LUHistoryLineChartAssistance> allHistoryLineChartAssistances = Lookup.getDefault().lookupAll(LUHistoryLineChartAssistance.class);
        for (LUHistoryLineChartAssistance lu : allHistoryLineChartAssistances) {
            m_pLUHistoryLineChartAssistance = lu;
            break;
        }

        if (m_pLUHistoryLineChartAssistance == null) {
            System.err.println("FactoryDiagramTopComponent: Cannot find and instance of LUHistoryLineChartAssistance");
        }

        // Initialize the FXML
        initFXML();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        topPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        LUaCheckBox = new javax.swing.JCheckBox();
        LUbCheckBox = new javax.swing.JCheckBox();
        LUcCheckBox = new javax.swing.JCheckBox();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        resolutionComboBox = new javax.swing.JComboBox<String>();
        mainPanel = new javax.swing.JPanel();

        jPanel3.setLayout(new java.awt.GridLayout(1, 3));

        LUaCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(LUaCheckBox, org.openide.util.NbBundle.getMessage(LUHistoryLineChartTopComponent.class, "LUHistoryLineChartTopComponent.LUaCheckBox.text")); // NOI18N
        LUaCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                LUaCheckBoxStateChanged(evt);
            }
        });
        jPanel3.add(LUaCheckBox);

        LUbCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(LUbCheckBox, org.openide.util.NbBundle.getMessage(LUHistoryLineChartTopComponent.class, "LUHistoryLineChartTopComponent.LUbCheckBox.text")); // NOI18N
        LUbCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                LUbCheckBoxStateChanged(evt);
            }
        });
        jPanel3.add(LUbCheckBox);

        LUcCheckBox.setSelected(true);
        org.openide.awt.Mnemonics.setLocalizedText(LUcCheckBox, org.openide.util.NbBundle.getMessage(LUHistoryLineChartTopComponent.class, "LUHistoryLineChartTopComponent.LUcCheckBox.text")); // NOI18N
        LUcCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                LUcCheckBoxStateChanged(evt);
            }
        });
        jPanel3.add(LUcCheckBox);

        jPanel4.setPreferredSize(new java.awt.Dimension(250, 20));

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(LUHistoryLineChartTopComponent.class, "LUHistoryLineChartTopComponent.jLabel1.text")); // NOI18N

        resolutionComboBox.setModel(new javax.swing.DefaultComboBoxModel<String>(new String[] { "2 seconds", "10 seconds", "30 seconds", "5 minutes" }));
        resolutionComboBox.setSelectedIndex(0);
        resolutionComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resolutionComboBoxActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(resolutionComboBox, 0, 170, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(resolutionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
        topPanel.setLayout(topPanelLayout);
        topPanelLayout.setHorizontalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelLayout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 530, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        topPanelLayout.setVerticalGroup(
            topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(topPanelLayout.createSequentialGroup()
                .addGap(0, 0, 0)
                .addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(38, 38, 38))
        );

        mainPanel.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(topPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(topPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 272, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void LUaCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_LUaCheckBoxStateChanged
        final boolean state = LUaCheckBox.isSelected();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                HistoryLineChartFXMLController controller = m_pLUHistoryLineChartAssistance.getHistoryLineChartFXMLController();
                if (controller != null) {
                    controller.switchDataSeriesStatus(1, state);
                }
            }
        });
    }//GEN-LAST:event_LUaCheckBoxStateChanged

    private void LUbCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_LUbCheckBoxStateChanged
        final boolean state = LUbCheckBox.isSelected();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                HistoryLineChartFXMLController controller = m_pLUHistoryLineChartAssistance.getHistoryLineChartFXMLController();
                if (controller != null) {
                    controller.switchDataSeriesStatus(2, state);
                }
            }
        });
    }//GEN-LAST:event_LUbCheckBoxStateChanged

    private void LUcCheckBoxStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_LUcCheckBoxStateChanged
        final boolean state = LUcCheckBox.isSelected();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                HistoryLineChartFXMLController controller = m_pLUHistoryLineChartAssistance.getHistoryLineChartFXMLController();
                if (controller != null) {
                    controller.switchDataSeriesStatus(3, state);
                }
            }
        });
    }//GEN-LAST:event_LUcCheckBoxStateChanged

    private void resolutionComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resolutionComboBoxActionPerformed
        final String resolution = (String) resolutionComboBox.getSelectedItem();
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                try {
                    HistoryLineChartFXMLController controller = m_pLUHistoryLineChartAssistance.getHistoryLineChartFXMLController();
                    if (resolution.equals("2 seconds")) {
                        if (controller != null) {
                            controller.changeChartMode(HistoryLineChartFXMLController.CHART_MODE_2_SECONDS);
                        }
                    } else if (resolution.equals("10 seconds")) {
                        if (controller != null) {
                            controller.changeChartMode(HistoryLineChartFXMLController.CHART_MODE_10_SECONDS);
                        }
                    } else if (resolution.equals("30 seconds")) {
                        if (controller != null) {
                            controller.changeChartMode(HistoryLineChartFXMLController.CHART_MODE_30_SECONDS);
                        }
                    } else if (resolution.equals("5 minutes")) {
                        if (controller != null) {
                            controller.changeChartMode(HistoryLineChartFXMLController.CHART_MODE_5_MINUTES);
                        }
                    }
                } catch (ChartModeException e) {
                    System.err.println(e.getMessage());
                }
            }
        });

    }//GEN-LAST:event_resolutionComboBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox LUaCheckBox;
    private javax.swing.JCheckBox LUbCheckBox;
    private javax.swing.JCheckBox LUcCheckBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JComboBox resolutionComboBox;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    private void initFXML() {
        m_pFXPanel = new JFXPanel();
        mainPanel.add(m_pFXPanel, BorderLayout.CENTER);
        Platform.setImplicitExit(false);
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                StackPane stackPane = m_pLUHistoryLineChartAssistance.getStackPane();
                Scene scene = new Scene(stackPane);
                m_pFXPanel.setScene(scene);

                HistoryLineChartFXMLController controller = m_pLUHistoryLineChartAssistance.getHistoryLineChartFXMLController();
                controller.initGraph(stackPane, "Line voltage");
            }
        });
    }
}
